//
//  SolarSeekStore.m
//  SolarSimple
//
//  Created by Marcelo Alves on 21/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SolarSeekStore.h"


@implementation SolarSeekStore
-(NSManagedObject *) find:(NSString *)entity withPredicate:(NSPredicate *)predicate {
	NSManagedObjectContext *moc = self.managedObjectContext;
  
	NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entity inManagedObjectContext:moc];
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	[request setEntity:entityDescription];
	
	[request setPredicate:predicate];
	
	NSError *error = nil;
	NSArray *array = [moc executeFetchRequest:request error:&error];
  
	if (array != nil) {
		if ([array count] == 0) {
			return nil;
		}
		
		if ([array count] > 1) {			
			NSLog(@"Warning : more than one %@ with the predicate '%@'", entity, predicate);
		}
		
		return [array objectAtIndex:0];
	}
	
	return nil;
}

-(NSManagedObject *)new:(NSString *)entity{
	NSManagedObjectContext *moc = self.managedObjectContext;
	NSManagedObject *item = [NSEntityDescription insertNewObjectForEntityForName:entity inManagedObjectContext:moc];
	
  return item;
}

-(SidebarItem *) newSidebarItemWithName:(NSString *)name parent:(SidebarItem *)parent tag:(UInt32) tag type:(UInt32) type order:(UInt32)order andIsHidden:(BOOL)isHidden{
  SidebarItem *item = [self new:@"SidebarItem"];
  
  item.name = name;
  item.parent = parent;
  
  item.tag = [NSNumber numberWithInt:tag];
  item.type = [NSNumber numberWithInt:type];
  item.order = [NSNumber numberWithInt:order];
  item.hidden = [NSNumber numberWithBool:isHidden];
  
  return item;
}
-(void) addSidebarItems {
	NSManagedObjectContext *moc = self.managedObjectContext;
  NSLog(@"Adding items to the sidebar");

  SidebarItem *network = [self newSidebarItemWithName:@"NETWORK" parent:nil tag:0 type:networkType order:0 andIsHidden:NO];
  [self newSidebarItemWithName:@"Downloads" parent:network tag:0 type:downloadMenuType order:1 andIsHidden:NO];
  [self newSidebarItemWithName:@"Uploads" parent:network tag:0 type:uploadMenuType order:2 andIsHidden:NO];

  SidebarItem *searches = [self newSidebarItemWithName:@"SEARCH FOR" parent:nil tag:0 type:searchType order:3 andIsHidden:NO];
	
	[moc save:nil];
}

- (id) init {
  self = [super init];
  if (self != nil) {
    lastSave = [NSDate date];
    ticketCache = [[NSMutableDictionary alloc] init];
    [self addSidebarItems];
  }
  return self;
}


-(NSManagedObjectContext *) managedObjectContext {
  return [[[NSApplication sharedApplication] delegate] managedObjectContext];
}

-(NSManagedObject *)findTicketWithTicketNumber:(UInt32) ticket {
  NSManagedObject *item = NULL;
  NSNumber *ticketNumber = [NSNumber numberWithInt:ticket];
  
  item = [ticketCache objectForKey:ticketNumber];
  
  if (!item) {  
    item = [self find:@"Ticket" withPredicate:[NSPredicate predicateWithFormat:@"number == %@", ticketNumber]];
    if (item) [ticketCache setObject:item forKey:ticketNumber];
  }

  return item;
}

-(void) save {
  NSTimeInterval timeElapsed = [lastSave timeIntervalSinceNow];
  if (timeElapsed < -5) {
    lastSave = [NSDate date];
    NSError *error = nil;
    [self.managedObjectContext save:&error];
    if (error) 
      NSLog(@"Error storing data : %@", error);
  }
}

@end
